<?php
require ('konfiguration.html');


$a=array('a'=>1,'b'=>2);
print $a{1};
function eval_frequency_and_order($frequency_array,$translations) //bei gleich häufigen Übersetzungen (mehr als einmal)
{ //Übersetzung die in allen Wörterbüchern durchschnittlich eher vorne liegt
   foreach ($frequency_array as $translation=>$count)
	{   if ($last_count<>'' and $last_count<>$count)
		{    $frequency_parted_array[]=$parted_array;
             $parted_array=array();	
		}
	$parted_array[$translation]=$count;
	$last_count=$count;
	}
	$frequency_parted_array[]=$parted_array;	
	print_r($frequency_parted_array);

	foreach ($frequency_parted_array as $parted_array)
	{
	  foreach ($parted_array as $translation=>$count)
		{   foreach ($translations as $translation_array)
				{    if (in_array($translation,$translation_array))
							{     $position_sum+=array_search($translation,$translation_array);
									$position_count+=1;
							}
				$position_average=	 $position_sum/$position_count;
				$position_array[$translation]=$position_average;		 
				}
		}
	print_r ($position_array);
	asort($position_array);
	foreach ($position_array as $translation=>$position_average)
		{    $translation_count = $frequency_array[$translation];
			$new_frequency_array[$translation]=$translation_count;

		}
	 $position_array=array();	
	}

	print_r($new_frequency_array);
	return $new_frequency_array;
}
function b(){print '<BR>';}
function get_value($count,$array)
{ $a=0; 
  foreach ($array as $value){if ($a==$count) return $value; $a+=1;}
}
function get_key($count,$array)
{ $a=0;
  foreach ($array as $key=>$value){if ($a==$count) return $key; $a+=1;}
}
function mysql_query_secure($query)
{
   $result=mysql_query($query);print $query;
   if ( ! $result ) { die('Error query : ' . mysql_error()); }
   return $result;
}

function num_dicts($first_lang,$second_lang)
{   $query=" SELECT DISTINCT `language_combo` 
                   FROM `alltranslations`
                   WHERE `language_combo` LIKE '{$first_lang}_{$second_lang}%'";print $inner_query;
   $result=mysql_query_secure($query);
   $num_dicts=mysql_num_rows($result);
   return $num_dicts;
}
function order_by_index($possible_translations)
{
   global $second_lang;
   foreach ($possible_translations as $possible_translation)
   {   $result=mysql_query_secure("SELECT `index` FROM mostcommonwords WHERE language='$second_lang' and word='$possible_translation'");
       if ($row=mysql_fetch_array($result)) $word_index=$row[0];else $word_index=30000;
	   $possible_array[$possible_translation]=$word_index;
   }
   asort($possible_array);
   return $possible_array;


}
function rank_remaining_translations($count_missing, $unfit,$possible_translations,$translations)
{  foreach ($possible_translations as $possible_translation)
   {  if (is_unfit($possible_translation,$unfit,$translations))$unfit_possible_translations[]=$possible_translation;
	  else $fit_possible_translations[]=$possible_translation;
   }
   $fit_possible_translations=order_by_index($fit_possible_translations);
   $unfit_possible_translations=order_by_index($unfit_possible_translations);
   print_r ($fit_possible_translations);print_r ($unfit_possible_translations); 
   $remaining=find_best($fit_possible_translations,$unfit_possible_translations,$count_missing);
   return $remaining;
}
function is_unfit($possible_translation,$unfit, $translations)
{   foreach ($translations as $key=>$translation)
    {   if (in_array($possible_translation,$translation) and in_array($key,$unfit)) return true;
    }
    return false;
}
function find_best($fit,$unfit,$count_missing)
{   $count_fit=0;$count_unfit=0;
    for ($a=0;$a<$count_missing;$a++)
    {    $fit_value=get_value($count_fit,$fit);$unfit_value=get_value($count_unfit,$unfit);
	    print $fit_value/($fit_value-$unfit_value);b();
		print $unfit_value;b();
        if ($fit_value<$unfit_value) {$remaining[]=get_key($count_fit,$fit);$count_fit+=1;}
        elseif ($fit_value/($fit_value-$unfit_value) < 4.5)  {$remaining[]=get_key($count_unfit,$unfit);$count_unfit+=1;}
		else {$remaining[]=get_key($count_fit,$fit);$count_fit+=1;}
    }
	return $remaining;
}

function eval_final_translations($frequency_array,$translations)
{
//1.�blichste �bersetzungen
//2.�bersetzungen, die h�ufiger im Sprachgebrauch sind

//Funktion zur Bestimmung der Abstands-Wertigkeit von 2 W�rtern derselben Liste
//index1/(index1-index2) > 4,5 //index1 ist die gr��ere Zahl  
$frequency_array=eval_frequency_and_order($frequency_array,$translations);
foreach ($frequency_array as $translation=>$translation_count)
//for ($a=0;$a<=2;$a++)
{
  if (count($final_translations)<3)
  {  if ($translation_count>1) //Vokabel kommt als �bersetzung in mehreren W�rterb�chern vor-> gute �bersetzung, �bernehmen
	 {  $final_translations[]=$translation;
 	 }
     else 
	 {
	    //herausfinden in welchen W�rterb�chern die bereits genommenen W�rter drin sind
		// diese W�rterb�cher sind 'weniger geeignet', k�nnen sich aber durch sehr gute Wortpositionierung
		//in mostcommonwords trotzdem noch einbauen
		$possible_translations[]=$translation;
		foreach ($final_translations as $final_translation)
        {   $count=0;
            foreach ($translations as $translation_array)
			{  if (in_array($final_translation,$translation_array)) $unfit[]=$count;
			   $count+=1;
			}
        }  		//$unfit=array_count_values($unfit); asort($unfit);//$unfit=array_keys($unfit);
     }
  }
}
$remaining=array();
$counted_translations=count($final_translations);
if ($counted_translations<3) 
{
   $remaining=rank_remaining_translations(3-$counted_translations,$unfit,$possible_translations,$translations);
}   
$final_translations=array_merge($final_translations,$remaining);
return $final_translations;
}
$frequency_array=array('por'=>2,'bien'=>2,'tu'=>1,'para'=>1,'con'=>1,);
$translations=array(0=>array('para','por'),1=>array('bien'),2=>array('tu'),3=>array('por'),4=>array('bien','con'),);
$arr=array('para','por','de','tu','bien','dfsd','con');

$first_lang="english";
$second_lang="spanish";
$unfit_array=array(1,3,2,5);
$fit=array('para'=>1,'por'=>15,'de'=>20);
$unfit=array('tu'=>2,'bien'=>4,'dfsd'=>8,'con'=>12);

$possible_translations=array('para','por','de','tu','bien','dfsd','con');
print_r (eval_final_translations($frequency_array,$translations));
//print_r(find_best($fit,$unfit,5));
//print is_unfit('yellow',$unfit_array,$translations);
//print_r (order_by_index($arr));
//print num_dicts($first_lang,$second_lang);
//rank_remaining_translations(4, $unfit_array,$possible_translations,$translations)
//durchschnittliche position finden

/*foreach ($frequency_array as $translation=>$count)
{   if ($last_count<>'' and $last_count<>$count)
    {    $frequency_parted_array[]=$parted_array;
         $parted_array=array();	
	}
	$parted_array[$translation]=$count;
	$last_count=$count;
}
$frequency_parted_array[]=$parted_array;
print_r($frequency_parted_array);

foreach ($frequency_parted_array as $parted_array)
{
foreach ($parted_array as $translation=>$count)
{   foreach ($translations as $translation_array)
    {    if (in_array($translation,$translation_array))
	     {     $position_sum+=array_search($translation,$translation_array);
		       $position_count+=1;
		 }
	$position_average=	 $position_sum/$position_count;
    $position_array[$translation]=$position_average;		 
    }
}
print_r ($position_array);
asort($position_array);
foreach ($position_array as $translation=>$position_average)
{    $translation_count = $frequency_array[$translation];
     $new_frequency_array[$translation]=$translation_count;

}
$position_array=array();	
}

print_r($new_frequency_array);*/

/*foreach ($frequency_array as $translation=>$count)
   {  
   $transaction_count+=1;
	  print '$translation'."$translation<BR>";
	  print '$last_translation'."$last_translation<BR>";
      if ($last_count==$count ) //beide Übersetzungen kommen gleich häufig in den verschiedenen Wörterbüchern vor
	  {
	     foreach ($translations as $translation_array) //es wird geschaut ob beide wörter drin sind, nur eins oder keines
		 {                                             // wenn beide drin sind kriegt das weiter vorne liegende einen vorzugspunkt
		    
			print_r($translation_array);
			if (in_array($last_translation,$translation_array)and in_array($translation,$translation_array)) 
			//das momentane Wort entspricht einem +Wert, das vorherige -Wert
			{
			   if (array_search($last_translation,$translation_array)< array_search($translation,$translation_array))
			           $last_or_current-=1; else $last_or_current+=1;
					   
			}
		    print '$last_or_current'."$last_or_current";
		}
	  }
	  if ($last_or_current>0) {
	  print '$last_or_current>0';
	  $new_frequency_array[]=$translation;
	  $new_frequency_array[]=$last_translation;
	  
	  $jump=true;}
	  else { if (!$jump and $transaction_count>1)
	  {print '!$jump';$new_frequency_array[]=$translation;}else $jump=False;
	  }
	  print_r($new_frequency_array);
	  $last_or_current=0;
      $last_translation=$translation;$last_count=$count;
   }
print_r($new_frequency_array);*/
?>